package com.xiaoximen;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.format.Time;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class TimePickerActivity extends Activity {

	private int mStartHour = 8;
	private int mEndHour = 18;
	
	Time mTime = new Time();

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_time_picker);
		
		Bundle data = getIntent().getExtras();
		int year = data.getInt(EditTimeListActivity.KEY_YEAR);
		int month = data.getInt(EditTimeListActivity.KEY_MONTH);
		int day = data.getInt(EditTimeListActivity.KEY_DAY);
		mTime.set(0, 0, 0, day, month-1, year);
		String weekDay = data.getString(EditTimeListActivity.KEY_WEEKDAY);
		String date = year + getString(R.string.year)
				+ month + getString(R.string.month)
				+ day + getString(R.string.day)
				+ weekDay;
		((TextView) findViewById(R.id.time_picker_date)).setText(date);
		
		RelativeLayout layout = (RelativeLayout) findViewById(R.id.time_picker_layout);
		layout.addView(new PickerView(this));
		
		findViewById(R.id.time_picker_confirm).setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = getIntent();
				Time start = new Time(mTime);
				start.hour = mStartHour;
				intent.putExtra(EditTimeListActivity.KEY_START_TIME, start.normalize(true));
				Time end = new Time(mTime);
				end.hour = mEndHour;
				intent.putExtra(EditTimeListActivity.KEY_END_TIME, end.normalize(true));
				setResult(RESULT_OK, intent);
				finish();
			}
		});
	}
	
	private class PickerView extends RelativeLayout {
		
		private final int PIN_HEIGHT = DisplayUtil.dp2px(getContext(), 35);
		private final int PIN_LINEHEIGHT = DisplayUtil.dp2px(getContext(), 5);
		private ImageView mStartPicker = new ImageView(getContext());
		private TextView mStartIndicator = new TextView(getContext());
		private ImageView mEndPicker = new ImageView(getContext());
		private TextView mEndIndicator = new TextView(getContext());
		private DayView mDayView = new DayView(getContext());
		private View mSpanView = new ImageView(getContext());
		private float mStartDownY, mEndDownY;

		public PickerView(Context context) {
			super(context);
			RelativeLayout.LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, 
					LayoutParams.MATCH_PARENT);
			params.addRule(RelativeLayout.ABOVE, R.id.time_picker_confirm);
			params.addRule(RelativeLayout.BELOW, R.id.time_picker_date);
			params.bottomMargin = DisplayUtil.dp2px(getContext(), 10);
			setLayoutParams(params);
			
			addView(mDayView);
			
			post(new Runnable() {

				@Override
				public void run() {
					initView();
				}
			});
		}
		
		private void initView() {
			RelativeLayout.LayoutParams params;

			params = new LayoutParams(LayoutParams.MATCH_PARENT, PIN_HEIGHT);
			params.alignWithParent = true;
			params.topMargin = yByStartHour(mStartHour);
			params.leftMargin = 0;
			mStartPicker.setLayoutParams(params);
			mStartPicker.setImageResource(R.drawable.pin_start);
			mStartPicker.setScaleType(ScaleType.FIT_END);
			addView(mStartPicker);
			mStartPicker.setOnTouchListener(new View.OnTouchListener() {
				
				@Override
				public boolean onTouch(View v, MotionEvent event) {
					RelativeLayout.LayoutParams params;
					switch (event.getAction()) {
					case MotionEvent.ACTION_DOWN:
						mStartDownY = event.getY();
						return true;
					case MotionEvent.ACTION_MOVE:
						float y = event.getY();
						params = (LayoutParams) mStartPicker.getLayoutParams();
						int newTopMargin = (int) (params.topMargin + y - mStartDownY);
						int h = startHourByY(newTopMargin);
						if (h >= 0 && h < mEndHour) {
							params.topMargin = newTopMargin; 
							mStartPicker.setLayoutParams(params);
							mStartHour = h;
							updateIndicators();
						}
						return true;
					case MotionEvent.ACTION_UP:
						params = (LayoutParams) mStartPicker.getLayoutParams();
						params.topMargin = yByStartHour(mStartHour);
						mStartPicker.setLayoutParams(params);
						return true;
					}
					return false;
				}
			});
			
			params = new LayoutParams(LayoutParams.MATCH_PARENT, PIN_HEIGHT);
			params.alignWithParent = true;
			params.topMargin = yByEndHour(mEndHour);
			params.leftMargin = 0;
			mEndPicker.setLayoutParams(params);
			mEndPicker.setImageResource(R.drawable.pin_end);
			mEndPicker.setScaleType(ScaleType.FIT_START);
			addView(mEndPicker);
			mEndPicker.setOnTouchListener(new View.OnTouchListener() {
				
				@Override
				public boolean onTouch(View v, MotionEvent event) {
					
					RelativeLayout.LayoutParams params;
					switch (event.getAction()) {
					case MotionEvent.ACTION_DOWN:
						mEndDownY = event.getY();
						return true;
					case MotionEvent.ACTION_MOVE:
						float y = event.getY();
						params = (LayoutParams) mEndPicker.getLayoutParams();
						int newTopMargin = (int) (params.topMargin + y - mEndDownY);
						int h = endHourByY(newTopMargin);
						if (h > mStartHour && h <= 24) {
							params.topMargin = newTopMargin; 
							mEndPicker.setLayoutParams(params);
							mEndHour = h;
							updateIndicators();
						}
						return true;
					case MotionEvent.ACTION_UP:
						params = (LayoutParams) mEndPicker.getLayoutParams();
						params.topMargin = yByEndHour(mEndHour);
						mEndPicker.setLayoutParams(params);
						return true;
					}
					return false;
				}
			});
			
			addView(mStartIndicator);
			addView(mEndIndicator);
			addView(mSpanView);
			updateIndicators();
		}
		
		private void updateIndicators() {
			mStartIndicator.setLayoutParams(mStartPicker.getLayoutParams());
			mStartIndicator.setText(mStartHour+":00");
			mStartIndicator.setTextColor(getResources().getColor(R.color.time_indicator_green));
			mStartIndicator.setTextSize(mDayView.mHourHeight);
			
			mEndIndicator.setLayoutParams(mEndPicker.getLayoutParams());
			mEndIndicator.setText(mEndHour+":00");
			mEndIndicator.setTextColor(getResources().getColor(R.color.time_indicator_green));
			mEndIndicator.setTextSize(mDayView.mHourHeight);
			
			int from = yByStartHour(mStartHour) + PIN_HEIGHT;
			int to = yByEndHour(mEndHour);
			LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, to-from);
			params.topMargin = from;
			mSpanView.setLayoutParams(params);
			mSpanView.setBackgroundColor(getResources().getColor(R.color.time_span));
		}
		
		private int yByStartHour(int startHour) {
			return mDayView.mHourHeight * (startHour+1) - PIN_HEIGHT + PIN_LINEHEIGHT/2;
		}
		
		private int yByEndHour(int endHour) {
			return mDayView.mHourHeight * (endHour+1) - PIN_LINEHEIGHT/2;
		}

		private int startHourByY(int topMargin) {
			float hours = (topMargin + PIN_HEIGHT - PIN_LINEHEIGHT/2)
					/(float)mDayView.mHourHeight;
			return Math.round(hours)-1;
		}
		
		private int endHourByY(int topMargin) {
			float hours = (topMargin + PIN_LINEHEIGHT/2)
					/(float)mDayView.mHourHeight;
			return Math.round(hours)-1;
		}
	}
	

	private class DayView extends RelativeLayout {

		int mHourHeight;

		public DayView(Context context) {
			super(context);
			
			LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,
					LayoutParams.MATCH_PARENT);
			setLayoutParams(params);
			
			post(new Runnable() {

				@Override
				public void run() {
					initView();
				}
			});
		}
		
		private void initView() {
			
			mHourHeight = getHeight()/26;
			setBackgroundColor(getResources().getColor(R.color.activity_background_light));

			RelativeLayout.LayoutParams params;
			for (int i = 0; i <= 24; i++) if ((i%3==0)){
				
				View v = new View(getContext());
				params = new LayoutParams(LayoutParams.MATCH_PARENT, 1);
				params.alignWithParent = true;
				params.leftMargin = 0;
				params.topMargin = mHourHeight * (i+1);
				v.setLayoutParams(params);
				v.setBackgroundColor(getResources().getColor(R.color.time_divider_shallow));
				addView(v);
			}
		}
	}
}
